package henry.lucky.alphay;

import java.awt.Container;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import henry.lucky.alphay.Domain.Prize;
import henry.lucky.alphay.db.DataBase;

@SuppressWarnings("serial")
public class PrizeSettings extends JDialog{

	JFrame frame;
	private Prize prize;
	private JTextField PrizeNameField = new JTextField(10);
	private JTextField NameField = new JTextField(10);
	private JTextField NumberField = new JTextField(10);
	private JTextField CycleIndexField = new JTextField(10);
	private JTextField PriceField = new JTextField(10);
	private JTextField PersonField = new JTextField(10);
	private JTextField LuckyIndexField = new JTextField(10);
	private DataBase base=new DataBase();

	public PrizeSettings(Frame owner,Prize prize) {
		super(owner);
		// TODO Auto-generated constructor stub
		this.prize=prize;
	}


	public PrizeSettings(Frame owner) {
		super(owner);
		// TODO Auto-generated constructor stub
	}

	public void setPrize(Prize prize) {
		this.prize = prize;
	}

	public void showPrizeSettings() {

//		dialog= new JDialog(frame);
//		dialog.setSize(300, 384);
//		dialog.setVisible(true);
		JLabel titleLabel = new JLabel();
		JLabel PrizeNameLabel = new JLabel("奖项");
		JLabel NameLabel = new JLabel("奖品");
		JLabel NumberLabel = new JLabel("抽奖数量");
		JLabel CycleIndexLabel = new JLabel("抽奖次数");
		JLabel PriceLabel = new JLabel("价值");
		JLabel PersonLabel = new JLabel("抽奖人");
		JLabel LuckyIndexLabel = new JLabel("抽奖顺序");
		JButton buttonOk = new JButton();
		JButton buttonCancel = new JButton("取消");
		
		if (prize != null) {
			PrizeNameField.setText(prize.getPrizeName());
			NameField.setText(prize.getName());
			NumberField.setText(String.valueOf(prize.getNumber()));
			CycleIndexField.setText(String.valueOf(prize.getCycleIndex()));
			PriceField.setText(String.valueOf(prize.getPrice()));
			PersonField.setText(prize.getPerson());
			LuckyIndexField.setText(String.valueOf(prize.getLuckyIndex()));
//			frame.setTitle("修改奖项");
			titleLabel.setText("修改奖项");
			buttonOk.setText("修改");
		} else {
//			frame.setTitle("添加奖项");
			titleLabel.setText("添加奖项");
			buttonOk.setText("添加");
		}
		Container container = getContentPane();
		container.add("North", titleLabel);
		GridLayout gridLayout = new GridLayout(8, 2);
		gridLayout.setHgap(10);
		gridLayout.setVgap(20);
		JPanel panelCenter = new JPanel(gridLayout);
		container.add("Center", panelCenter);
		panelCenter.add(PrizeNameLabel);
		panelCenter.add(PrizeNameField);
		panelCenter.add(NameLabel);
		panelCenter.add(NameField);
		panelCenter.add(NumberLabel);
		panelCenter.add(NumberField);
		panelCenter.add(CycleIndexLabel);
		panelCenter.add(CycleIndexField);
		panelCenter.add(PriceLabel);
		panelCenter.add(PriceField);
		panelCenter.add(PersonLabel);
		panelCenter.add(PersonField);
		panelCenter.add(LuckyIndexLabel);
		panelCenter.add(LuckyIndexField);
		panelCenter.add(buttonOk);
		panelCenter.add(buttonCancel);
		buttonOk.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				addPrize();
				dispose();
			}
		});
		
	}

	private void addPrize() {

		Prize prizeResult=new Prize(PrizeNameField.getText(),
				NameField.getText(), Integer.valueOf(NumberField
						.getText()), Integer.valueOf(CycleIndexField
						.getText()), Integer.valueOf(PriceField.getText()),
				PersonField.getText(), Integer.valueOf(LuckyIndexField.getText()));
		if (prize != null) {
			prizeResult.setId(prize.getId());
			prizeResult.setCanType(prize.getCanType());	
			base.updatePrize(prizeResult);
		} else{
			base.insertPrize(prizeResult);
		}
	}
}